package shortcircuit.engine;

import java.awt.GridLayout;
import java.awt.event.*;

import javax.swing.JLabel;
import javax.swing.JPanel;

import shortcircuit.*;
import shortcircuit.basics.Environment;
import shortcircuit.basics.EnvironmentPane;
import shortcircuit.basics.PowerUp;
import shortcircuit.basics.powerup.*;
import shortcircuit.graphics.*;
import shortcircuit.utilities.FileIO;

/**
 * This class is a subclass of the more generalized version, GameEngine.
 * The puzzle engine runs all of the behind-the-scenes acts of Puzzle
 * Mode in this project.<p>
 * Functions include the storing of levels and their locked status as 
 * well as the ability to create maps from the pre-read map files.
 * 
 * @author Kevin Lewi
 */
public class PuzzleEngine extends GameEngine
{
	boolean[] _Levels;
	FileIO mapReader;
	

	public PuzzleEngine(Display theEngine, int startingLevel)
	{
		super(theEngine, 0);
		_Display.clearScreens();
		beginLevel(startingLevel);
	}
	
	public PuzzleEngine(Display theEngine)
	{
		super(theEngine, 0);
		
		_Display.clearScreens();
		_Display.directlyAddScreen(new PuzzleLevelScreen(this, fileSaver.getLevelStatuses()), 0);
	}
	
	/**
	 * Begins a level by replacing the correct listeners and reading in the
	 * correct map specified by the level passed in as a parameter.  Also
	 * calls Display to show the new window.
	 * 
	 * @param theLevel The current level
	 * @author Kevin Lewi
	 */
	public void beginLevel(int theLevel)
	{
		try
		{
		_Level = theLevel;
		popUpScreen = new BeginGameScreen(PopUpScreen.PUZZLE, this);
		mapReader = new FileIO("maps/map" + theLevel + ".scmap", FileIO.READ);
		Environment env = mapReader.readMap();
		numberOfMoves = mapReader.getNumberOfMoves();
		blocksToRemove = mapReader.getBlocksToRemove();
		int[] powerUpArray = mapReader.powerUpArray;
		for(int i = 0; i < powerUpArray.length; i++)
		{
			PowerUp thePowerUp = new Null();
			switch(powerUpArray[i])
			{
				case 1: thePowerUp = new Bomb();
			}
			powerUps.add(i, thePowerUp);
		}
		EnvironmentPane ep = new EnvironmentPane(env);
		super.createMap(ep);
		_Display.clearScreens();
		gameScreen = new GameScreen(this);
		statsScreen = new StatsScreen(this);
		_Display.directlyAddScreen(popUpScreen, 0);
		_Display.addScreen(gameScreen);
		_Display.addScreen(statsScreen);
		_Display.directlyAddScreen(MainMenuScreen.bgPanel, 3);
		}
		catch(Exception e){}
	}
	
	public void update()
	{
		super.update();
		if(popUpScreen != null && popUpScreen.doneLoading)
		{
			_Display.removeScreen(popUpScreen);
			gameScreen.addListeners();
		}
	}
	
	/**
	 * Waits for at least the specified number of blocks to be removed before ending
	 * the level successfully, or waits until the user has used up all of his moves
	 * and ends the level unsuccessfully. 
	 * 
	 * @author Kevin Lewi
	 */
	public void checkEndGame()
	{
		if(blocksRemoved >= blocksToRemove)
		{
			fileSaver.setLevelStatus(_Level, 2);
			if(fileSaver.getLevelStatus(_Level + 1) != 2)
				fileSaver.setLevelStatus(_Level + 1, 1);
			if(fileSaver.getLevelStatus(_Level + 2) != 2)
				fileSaver.setLevelStatus(_Level + 2, 1);
			gamePulse.setInterrupted(true);
			gamePulse.setEnded(true);
			gameScreen.removeListeners();
			popUpScreen = new EndGameScreen(0, true, this);
			_Display.directlyAddScreen(popUpScreen, 0);
		}
		else
		{
			checkForCompletePath();
			addToCompletePath();
			if(getMovesLeft() == 0 && allCompletePaths.size() == 0
					&& environmentPane.getFallingBlocksSize() == 0
					&& fallingBlocksSize == 0)
			{
				gamePulse.setInterrupted(true);
				gamePulse.setEnded(true);
				gameScreen.removeListeners();
				popUpScreen = new EndGameScreen(0, false, this);
				_Display.directlyAddScreen(popUpScreen, 0);
			}
		}
		
	}
}
